﻿//-----------------------------------------------------------------------
// <copyright file="Repository.cs" company="deixei.com">
//     Copyright (c) deixei.com. All rights reserved.
// </copyright>
// <author>Marcio Parente</author>
//----------------------------------------------------------------------
[module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "Reviewed.")]

namespace Deixei.Infrastructure
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Data.Objects;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Text;

    /// <summary>
    /// Repository of Model Entities
    /// </summary>
    /// <typeparam name="T">Model Entities</typeparam>
    public class Repository<T> : IRepository<T> where T : class
    {
        /// <summary>
        /// object set
        /// </summary>
        private IDbSet<T> objectset;

        /// <summary>
        /// Gets the object set.
        /// </summary>
        /// <value>
        /// The object set.
        /// </value>
        public IDbSet<T> ObjectSet
        {
            get 
            {
                if (this.objectset == null)
                {
                    this.objectset = this.UnitOfWork.Context.Set<T>();
                }

                return this.objectset;
            }
        }

        /// <summary>
        /// Gets or sets the unit of work.
        /// </summary>
        /// <value>
        /// The unit of work.
        /// </value>
        public IUnitOfWork UnitOfWork { get; set; }

        /// <summary>
        /// Alls this instance.
        /// </summary>
        /// <returns>Queryable entity</returns>
        public virtual IQueryable<T> All()
        {
            return this.ObjectSet.AsQueryable();
        }

        /// <summary>
        /// Where the specified expression.
        /// </summary>
        /// <param name="expression">The expression.</param>
        /// <returns>Queryable entity</returns>
        public IQueryable<T> Where(Expression<Func<T, bool>> expression)
        {
            return this.ObjectSet.Where(expression);
        }

        /// <summary>
        /// Adds the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Add(T entity)
        {
            this.ObjectSet.Add(entity);
        }

        /// <summary>
        /// Deletes the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Delete(T entity)
        {
            this.ObjectSet.Remove(entity);
        }

        /// <summary>
        /// Updates the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Update(T entity)
        {
            this.ObjectSet.Attach(entity);
            this.UnitOfWork.Context.Entry(entity).State = EntityState.Modified;
        }
    }
}
